package com.teckteam.ptvsystem.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import com.teckteam.ptvsystem.controller.UpdateTimetableListener;
import com.teckteam.ptvsystem.model.AbstractStop;
import com.teckteam.ptvsystem.model.ResultNode;
import com.teckteam.ptvsystem.model.facade.PTVModel;
import com.teckteam.ptvsystem.model.facade.PTVSystem;

/**
 * Main UI frame class for the PTVsystem 
 *
 */
public class PTVUserInterface extends JFrame
{
   
   // Frame controls
   class CloseDialog extends WindowAdapter
   {
      public void windowClosing(WindowEvent e)
      {
         int response = JOptionPane.showOptionDialog(PTVUserInterface.this,
               "Do you want to exit this application?", "Exit",
               JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
               null, null);
         
         switch (response)
         {
            case JOptionPane.YES_OPTION:
               System.err.println("Exiting...");
               System.exit(0);
               break;
            case JOptionPane.NO_OPTION:
               System.err.println("Not exiting...");
               break;
         }
      }
   }
   
   private PTVModel  model;
   private JPanel    drawPanel, top, bottom;
   private JLabel    title = new JLabel("Welcome to TeckTeam's PTV Timetable and Journey Planner system."),
                     logo = new JLabel(new ImageIcon("TeckTeamLogo.gif"));
   private Container contentPane;
   private JButton   jpButton, bttButton, updButton;
   
   private ArrayList<String>       suburbList;
   private ArrayList<AbstractStop> stopList;
   
   //Singleton pattern
   private static PTVUserInterface instance = null;
   
   private PTVUserInterface()
   {
      //Use look and feel of OS
      try
      {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e)
      {
         //Do not care.
      }
         
      setSize(500, 300);
      setLocationRelativeTo(null);
      setTitle("TeckTeam's Public Transport Victoria Journey Planner");
      setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
      addWindowListener(new CloseDialog());
      setBackground(Color.GRAY);
      
      drawPanel = new JPanel(new BorderLayout());
      drawPanel.setBorder(BorderFactory.createLoweredBevelBorder());
      contentPane = this.getContentPane();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(drawPanel, BorderLayout.CENTER);
      contentPane.add(title, BorderLayout.PAGE_START);
      
      top = new JPanel();
      bottom = new JPanel();
      
      title.setHorizontalAlignment(SwingConstants.CENTER);
      
      jpButton = new JButton("Journey Planner");
      jpButton.addActionListener(new ActionListener() 
      {
         public void actionPerformed(ActionEvent e)
         {
            openJourneyPlanner();
         }
      });
      
      bttButton = new JButton("Browse Timetable");
      bttButton.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            searchTimetable();
         }
      });
      
      updButton = new JButton("Update");
      updButton.addActionListener(new UpdateTimetableListener());
      
      top.add(jpButton);
      top.add(bttButton);
      top.add(updButton);
      
      drawPanel.add(top, BorderLayout.PAGE_START);
      drawPanel.add(bottom, BorderLayout.CENTER);
      drawPanel.add(logo, BorderLayout.SOUTH);
      
      // model = new PTVSystem();
      
      this.setVisible(true);
   }
   
   public static PTVUserInterface getInstance()
   {
      if(instance==null)
         instance = new PTVUserInterface();
      
      return instance;
   }
   
   public PTVModel getApplicationModel()
   {
      return model;
   }
   
   public JPanel getDrawPanel()
   {
      return drawPanel;
   }
   
   private void openJourneyPlanner()
   {
      if (!model.isEmpty())
         new JourneyPlanner(suburbList, stopList);
      else
         JOptionPane.showOptionDialog(null,
               "There are no timetables loaded in the system.\n",
               "Empty database", JOptionPane.DEFAULT_OPTION,
               JOptionPane.WARNING_MESSAGE, null, null, null);
   }
   
   private void searchTimetable()
   {
      if (!model.isEmpty())
         new TimetableBrowser(this);
      else
         JOptionPane.showOptionDialog(null,
               "There are no timetables loaded in the system.\n",
               "Empty database", JOptionPane.DEFAULT_OPTION,
               JOptionPane.WARNING_MESSAGE, null, null, null);
   }
   
   /**
    * Used to refresh the interface if the model has been refreshed
    */
   public void refreshView()
   {
      this.model = PTVSystem.getInstance();
      
      if(!this.model.isEmpty())
      {
         this.suburbList = this.model.getAllSuburbs();
         this.stopList = this.model.getAllStops();
         Collections.sort(suburbList);
      }
   }
   
   /**
    * Show Journey Planner results after completing the form and system finishes.
    * TODO: method location reserved for integration to frame.
    * 
    * @param resultsList
    */
   public void showResults(ArrayList<ResultNode> resultsList)
   {
      new JourneyPlannerResults(resultsList);
   }
}
